(function() {
  app.controller('registerCtrl', function($rootScope, $scope, $state, tradeApi, alertTip, $interval, $timeout, $location) {
    $scope.client = {
      member_account: '1000'
    };
    $scope.get_verifycode = '';
    var num = 60;
    var count = 0; // 防止多次点击
    $scope.get_code_color = 1;
    $scope.code_tip = '获取验证码';
    $scope.init = function() {
      if ($state.params.member_account) {
        $scope.client.member_account = $state.params.member_account;
        $scope.client.proxy_account = $state.params.proxy_account;
      }
    };

    $scope.get_code = function() {
      if (!$scope.client.mobile) {
        alertTip.open({
          msg: '请先填写手机号！'
        });
        return;
      }

      count++;
      if (count !== 1) { return; }

      tradeApi.query({
        funcId: '300021',
        data: {
          auth_type: '0',
          mobile: $scope.client.mobile
        }
      })

      .then(function(res) {
          $scope.get_code_color = 0;
          var timer = $interval(function() {
            num--;
            $scope.code_tip = num + '秒后重新获取';
          }, 1000, 59).then(function() {
            $scope.get_code_color = 1;
            $scope.code_tip = '重新获取验证码';
            num = 60;
            count = 0;
          });
        })
        .catch(function() {
          count = 0;
        });
    };

    $scope.ok = function() {
      if (!$scope.checked) {
        alertTip.open({
          msg: '请先阅读条款!',
        });
        return;
      }
      if ($location.search()['recommend_id']) $scope.client.recommend_id = $location.search()['recommend_id'];
      tradeApi.query({
        funcId: '300000',
        data: $scope.client
      }).then(function(res) {
        alertTip.open({
          msg: '注册成功!',
          state: 1,
        });
        $timeout(function() {
          $state.go('login');
        }, 2000);
      });
    };

  });
})();